package hashtab;

import java.util.Random;
import java.util.Scanner;

public class HashTabDemo {
    public static void main(String[] args) {
    /*    Emp[] e = new Emp[1000];
        Random ramdom = new Random(47);
        for (int i = 0; i < 1000; i++) {
            e[i]=new Emp(ramdom.nextInt(1000),"sun");
        }
        hashTab hash = new hashTab(20);
        for (int i = 0; i < 1000; i++) {
            hash.add(e[i]);
        }
        hash.list();*/
        Scanner sc  = new Scanner(System.in);
        hashTab hash = new hashTab(5);
        int id;
        String name;
        String str="";
        while(true){
            System.out.println("add:添加成员");
            System.out.println("select:根据id号查找成员");
            System.out.println("list:查看成员");
            System.out.println("exit:退出");
            str=sc.next();
            switch (str){
                case "add":
                    System.out.println("请输入id");
                    id=sc.nextInt();
                    System.out.println("请输入姓名");
                    name=sc.next();
                    hash.add(new Emp(id,name));
                    break;
                case "list":
                    hash.list();
                    break;
                case "select":
                    System.out.println("请输入id号");
                    id=sc.nextInt();
                    hash.select(id);
                    break ;
                case "del":
                    System.out.println("请输入id号");
                    id=sc.nextInt();
                    hash.del(id);
                    break ;
                case "exit":
                    sc.close();
                    return;
                default:
                    System.out.println("输入错误！请重新输入");
                    break;
            }
        }
    }

}
//hash表
class hashTab{
    EmpLinkList[] emplinklist;
    int size;
    hashTab(int size){
        this.size=size;
        emplinklist= new EmpLinkList[size];
        for (int i = 0;i<size;i++){
            emplinklist[i] = new EmpLinkList();
        }
    }
    //添加
    public void add(Emp emp){
        int no=hashFun(emp.id);
        emplinklist[no].add(emp);
    }
    //遍历
    public void list(){
        for (int i = 0; i < size; i++) {
            emplinklist[i].list(i+1);
        }
    }
    public void select(int id){
        int no=hashFun(id);
        Emp temp = emplinklist[no].select(id);
        if(temp==null){
            System.out.println("此id不存在!!");
            return;
        }
        System.out.println(temp);
    }
    public void del(int id){
        int no=hashFun(id);
        if(emplinklist[no].del(id)){
            System.out.println("删除成功");
        }else {
            System.out.println("此id不存在，删除失败");
        }
    }
    public int hashFun(int id){
        return id%size;
    }

}
//链表
class EmpLinkList{
    private Emp head;
    public void add(Emp emp){
        if(head==null){
            head=emp;
            return;
        }
        Emp temp=head;
        while(true){
           if(temp.next==null) {
               temp.next=emp;
               break;
           }
           temp=temp.next;
        }
    }
    //查询
    public Emp select(int id){
        Emp temp =head;
        while(true){
            if(temp.id==id || temp==null){
                return temp;
            }
            temp=temp.next;
        }
    }
    public boolean del(int id){
        if(head.id==id){
            head=head.next;
            return true;
        }
        Emp temp =head;
        while(true){
            if(temp.next==null){
                return false;
            }
            if(temp.next.id==id){
                temp.next=temp.next.next;
                return true;
            }
            temp=temp.next;
        }
    }
    public void list(int no){
        Emp temp=head;
        if(temp==null){
            System.out.println("第"+no+"条链表为null");
            return ;
        }
        System.out.print("第"+no+"条链表为=》");
        while(temp!=null){
            System.out.print("id="+temp.id+" name="+ temp.name+"  ");
            temp=temp.next;
        }
        System.out.println();
    }
}
class Emp{
    public int id;
    public String name;
    public Emp next;
    Emp(int id,String name){
        this.name=name;
        this.id=id;
    }
    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}